<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="开卡时间" prop="createAt">
        <el-date-picker clearable
                        v-model="queryParams.createAt"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择开卡时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <el-table v-loading="loading" :data="cardList" >
      <el-table-column label="教练姓名" align="center" prop="coachId" >
        <template slot-scope="scope">
          {{scope.row.easyCoach.name}}
        </template>
      </el-table-column>
      <el-table-column label="课程卡名称" align="center" prop="courseCardId" >
        <template slot-scope="scope">
          {{scope.row.easyCourseCard.name}}
        </template>
      </el-table-column>
      <el-table-column label="开卡用户" align="center" prop="userId" >
        <template slot-scope="scope">
          {{scope.row.easyUser.nickName}}
        </template>
      </el-table-column>
      <el-table-column label="上课次数" align="center" prop="attendNum" />
      <el-table-column label="课程状态" align="center" prop="status" >
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.status==1">正常</el-tag>
          <el-tag type="warning" v-if="scope.row.status==-1">过期</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="开卡时间" align="center" prop="createAt" width="180"/>
      <el-table-column label="生效时间" align="center" prop="startAt" />
      <el-table-column label="失效时间" align="center" prop="endAt" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['system:card:edit']"
          >更换教练
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 更换教练弹出层 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="教练" prop="coachId">
          <el-select v-model="form.coachId" filterable @change="coachChange" placeholder="请选择教练">
            <el-option
              v-for="item in coachList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="课程" prop="courseId">
          <el-select v-model="form.courseId" filterable placeholder="请选择课程">
            <el-option
              v-for="item in courseList"
              :key="item.id"
              :label="item.name"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCard,getCourseCard,updateCoach} from "@/api/debao/userCourseCard";
import { alllistCoach } from '@/api/debao/coach'
export default {
  name: "Card",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 弹出层标题
      title: '',
      // 总条数
      total: 0,
      // 用户的天卡表格数据
      cardList: [],
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        coachId: null,
        courseCardId: null,
        userId: null,
        startAt: null,
        endAt: null,
        createAt: null,
        attendNum: null,
        updateAt: null,
        status: null,
        isDel: null
      },
      // 表单参数
      form: {},
      coachList: [],
      courseList: [],
      rules: {
        userId: [
          { required: true, message: '用户ID不能为空', trigger: 'blur' }
        ],
        courseCardId: [
          { required: true, message: '课程ID不能为空', trigger: 'blur' }
        ],
        coachId: [
          { required: true, message: '教练ID不能为空', trigger: 'blur' }
        ]
      }
    };
  },
  created() {
    this.getList();
    this.getCoachList()
  },
  methods: {
    /** 查询用户的天卡列表 */
    getList() {
      this.loading = true;
      listCard(this.queryParams).then(response => {
        this.cardList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    //获取所有教练列表
    getCoachList() {
      alllistCoach().then(res => {
        this.coachList = res.data
      })
    },
    coachChange(e) {
      getCourseCard(e,this.form.typeId).then(res => {
        this.courseList = res.data
      })
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        courseId: null,
        coachId: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      this.form.id = row.id
      this.form.typeId = row.easyCourseCard.typeId
      console.info(this.form)
      this.open = true
      this.title = '教练调整'
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs['form'].validate(valid => {
        if (valid) {
          updateCoach(this.form).then(response => {
            this.$modal.msgSuccess('修改成功')
            this.open = false
            this.getList()
          })
        }
      })
    }
  }
};
</script>
